package org.x.user.mapper;

import cn.dev33.satoken.stp.StpUtil;
import com.mybatisflex.core.BaseMapper;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.x.user.dto.CodeDTO;
import org.x.user.entity.SysAclEntity;
import org.x.user.entity.SysRoleAclEntity;
import org.x.user.entity.SysRoleUserEntity;

import java.util.List;

public interface CodeMapper extends BaseMapper<CodeDTO> {

    /**
     * @return
     */
    default List<CodeDTO> getCode(){
            QueryWrapper queryWrapper = new QueryWrapper();

            queryWrapper.select(SysAclEntity::getCode,SysAclEntity::getName,SysAclEntity::getId).from(SysRoleUserEntity.class)
                    .leftJoin(SysRoleAclEntity.class).on(SysRoleUserEntity::getRoleId, SysRoleAclEntity::getRoleId)
                    .leftJoin(SysAclEntity.class).on(SysRoleAclEntity::getAclId, SysAclEntity::getId)
                    .where("user_id = "+ StpUtil.getLoginIdAsLong());

        List<CodeDTO> codeDTOS = this.selectListByQuery(queryWrapper);

        return codeDTOS;

    }

    @Select("SELECT `sys_acl`.`code`, `sys_acl`.`name`, `sys_acl`.`id` FROM `sys_role_user` \n" +
            "  LEFT JOIN `sys_role_acl` ON `sys_role_user`.`role_id` = `sys_role_acl`.`role_id` \n" +
            "  LEFT JOIN `sys_acl` ON `sys_role_acl`.`acl_id` = `sys_acl`.`id` WHERE user_id = #{userId}\n")
    List<CodeDTO> getCode2(@Param("userId") Long userId);
}
